<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/2/19
 * Time: 10:09
 */

namespace app\admin\model;


use think\Model;

class BaseModel extends Model
{
    /**
     * 获取分页列表
     * @param $page
     * @param $pageSize
     * @param $where
     * @param $order
     * @return array
     * @throws \think\exception\DbException
     */
    public static function getPageListByPage($page,$pageSize,$where,$order,$field)
    {
        $list = self::where($where)
            ->field($field)
            ->order($order)
            ->paginate(array('list_rows' => $pageSize, 'page' => $page))
            ->toArray();
        return $list;
    }
    /**
     * 获取字段值
     * @param string $field
     * @param $where
     * @param bool $isList
     * @return array|null|\PDOStatement|string|\think\Collection|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getFieldValue($field = "*",$where,$isList = false,$order=['id'=>'asc']){
        if(!$isList){
            $list = self::where($where)
                ->field($field)
                ->find();
        }else{
            $list = self::where($where)
                ->field($field)
                ->order($order)
                ->select();
        }
        return $list;
    }
    /**
     * 软删除
     * @param $id
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function softDel($id,$field='',$val='')
    {
        if($field != '')
        {
            $data = [];
            $data['is_del'] = 2;
        }else{
            $data[$field] = $val;
        }
        $res = self::where("id",$id)
            ->update($data);
        return $res;
    }

    /**
     * 更新
     * @param $where
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function updateOneData($where,$data)
    {
        $res = self::where($where)
            ->update($data);
        return $res;
    }

}